import Util from '../../../utils/utils';
import {
  HomeHttp
} from '../../../api/home.js'
import Dialog from "../../../dist/dialog/dialog";
const homeHttp = new HomeHttp()
Page({
  /**
   * 页面的初始数据
   */
  initStatus: false,
  updateAddressOrderId: 0,
  data: {
    goodsList: [],
    page: 1,
    pageSize: 10,
    totalNum: 0,
    orderStatus: 4
  },
  initPage: function (options) {
    this.httpGetOrderList()
  },
  //点击顶部tabs
  onTabClick(e) {
    let status = e.detail.name
    this.setData({
      page: 1,
      orderStatus: status
    })
    this.httpGetOrderList()
  },
  // 更新地址
  upAddress(_detail) {
    let self = this;
    self.addressDetail = _detail;
  },
  onShow() {
    let self = this;
    if (this.addressDetail) {
      let _detail = this.addressDetail;
      Dialog.confirm({
        message: '确定中奖物品邮寄到这个地址？',
        confirmButtonText: "立即提交",
        cancelButtonText: "我再选选"
      }).then((res) => {
        Util.request({
          url: '/order/updateGiftOrderAddress',
          loading: true,
          isJava: true,
          data: {
            unionOrderId: self.updateAddressOrderId,
            address: _detail.address,
            receiveName: _detail.user_name,
            phoneNumber: _detail.phone_number,
          },
          handle: true,
          callback: function (result) {
            if (result.status === 'ok') {
              Util.msg('订单收货地址提交成功,请等待工作人员处理');
              setTimeout(function () {
                self.httpGetOrderList();
              }, 300);
            } else {
              Util.msg(result.msg);
            }
            self.addressDetail = null;
          },
          error: function (result) {
            if (result.msg) {
              Util.msg(result.msg);
            } else {
              Util.msg('未知错误！');
            }
            self.addressDetail = null;
          }
        });
      }).catch((res) => {
        console.log(res);
        self.addressDetail = null;
      });
    }
  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.httpGetOrderList();
  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    const { goodsList, totalNum } = this.data
    if (goodsList.length < totalNum) {
      this.setData({
        page: this.data.page + 1
      })
      this.httpGetOrderList()
    }
  },
  getAddress: function (e) {
    let storeId = e.detail.storeId;
    this.updateAddressOrderId = e.detail.orderId;
    Util.toPage('/pages/me/address/manage/manage?store_id=' + storeId);
  },
  //获取订单列表
  httpGetOrderList() {
    let {
      goodsList,
      page,
      pageSize,
      orderStatus
    } = this.data;
    let list = []
    homeHttp.getOrderList({
      'pageNum': page,
      'pageSize': pageSize,
      'orderMainType': 2, // 订单主类型:1.门店订单，2.奖品订单，3秒杀订单,默认为1
      // 订单类型(orderStatus):
      // >>> orderMainType=1 时: 0:全部订单(默认),1:待付款,2:待收货,3:已收货,4:退货/退款
      // >>> orderMainType=2 时: 3:已完成,4:未完成
      // >>> orderMainType=3 时: 1:待付款,2:待发货,3:已完成
      'orderStatus': orderStatus * 1
    }).then((res) => {
      wx.stopPullDownRefresh()
      this.setData({
        goodsList: page === 1 ? res.list : goodsList.concat(res.list),
        totalNum: res.total
      })
    })
  },
});